package klab;

import java.io.*;

public class BatchMaker {
	private static BatchMaker instance = null;
	private ReadEnv re = ReadEnv.getInstance();
	private SysOperacije sysop;
	private BatchMaker() {
		super();
		sysop=SysOperacije.getInstance();
	}
	
	public static BatchMaker getInstance(){
		if (null == instance)
			instance = new BatchMaker();
		return instance;
	}
	/**
	 * Pravi batch za kopiranje fajlova u smeru zavisnom od parametra vrsta
	 * @param par - da li se kopiraju parni
	 * @param nepar - da li se kopiraju neparni
	 * @param ime - naziv batch fajla
	 * @param vrsta - UPLOAD, ili DOWNLOAD konstanta klase SysOperacije
	 * @throws IOException - ako ima problema sa fajl sistemom
	 */
	protected void mkCopyBatch(boolean par, boolean nepar, String ime, int vrsta)throws IOException{ 
		PrintWriter out=null;
		out = new PrintWriter(new BufferedWriter(new FileWriter(ime)));
		String sourceN="", sourceP="", destN="", destP="";
		for (int i=0; i< sysop.racunari.size(); i++){
			if (sysop.racunari.get(i).isSelected()){
				if (vrsta==SysOperacije.UPLOAD ){
					sourceN= KLForma.getInstance().source.neparniPath.getText();
					sourceP= KLForma.getInstance().source.parniPath.getText();
					destN  = "\\\\"+sysop.racunari.get(i).getText()+"\\"+ re.getMainHomePath(true) +"\\"+
					         KLForma.getInstance().target.neparniPath.getText();
					destP  = "\\\\"+sysop.racunari.get(i).getText()+"\\"+ re.getMainHomePath(true) +"\\"+
					         KLForma.getInstance().target.parniPath.getText();
					}else if (vrsta==SysOperacije.DOWNLOAD){
						sourceN= "\\\\"+sysop.racunari.get(i).getText()+"\\"+ re.getMainHomePath(true) +"\\"+
								 KLForma.getInstance().target.neparniPath.getText();
						sourceP= "\\\\"+sysop.racunari.get(i).getText()+"\\"+ re.getMainHomePath(true) +"\\"+
						         KLForma.getInstance().target.parniPath.getText();
						destN  = KLForma.getInstance().destination.neparniPath.getText()+
								 "\\n_"+sysop.racunari.get(i).getText();
						destP  = KLForma.getInstance().destination.parniPath.getText()+
								 "\\p_"+sysop.racunari.get(i).getText();
					}
				// Obrnuto od matematike jer se krece od indeksa 0 koji je parni
				if ((i % 2 != 0) && par){
					out.println("xcopy /E /C /I /H /K /Y \""+sourceP+"\" \""+destP+"\"");
				}
				if ((i % 2 ==0) && nepar){
					out.println("xcopy /E /C /I /H /K /Y \""+sourceN+"\" \""+destN+"\"");
				}
			}
		}
		out.close();
	}
	/**
	 * Pravi batch fajl pomocu koga se selektovani racunari gase/restartuju/ponistava komanda gasenja i restartovanja
	 * @param ime - naziv batch fajla
	 * @param vrsta - SHUTDOWN, RESTART, ili ABORT_SHUTDOWN konstanta klase SysOperacije
	 * @throws IOException
	 */
	protected void mkGasiBatch(String ime, int vrsta) throws IOException{
		PrintWriter out=null;
		out = new PrintWriter(new BufferedWriter(new FileWriter(ime)));
		String komanda = "";
		String poruka  = "";
		switch (vrsta){
			case SysOperacije.SHUTDOWN:	komanda = "-s";
										poruka = " -t "+ sysop.GASENJE_PERIOD +
										" -f -c \""+sysop.PORUKA_ZA_GASENJE+"\" -d up:1:256";
										break;
			case SysOperacije.RESTART:	komanda = "-r";
										poruka = " -t "+ sysop.RESTART_PERIOD +
										" -f -c \""+sysop.PORUKA_ZA_RESTART+"\" -d up:1:256";
										break;
			case SysOperacije.ABORT_SHUTDOWN: komanda = "-a";
		}
		for (int i=0; i < sysop.racunari.size() ;i++){
			if (sysop.racunari.get(i).isSelected()){
				out.println( "shutdown "+komanda+" -m \\\\"+ sysop.racunari.get(i).getText() + poruka);
			}
		}
		out.close();
	}
	/**
	 * Generise batch fajl kojime se brisu direktorijumi i sadrzaji kanti za djubre na klijentima
	 * @param par - da li se brisu parni
	 * @param nepar - da li se brisu neparni
	 * @param ime - naziv batch fajla
	 * @throws IOException
	 */
	protected void mkBrisiBatch(boolean par, boolean nepar, String ime)throws IOException{
		PrintWriter out=null;
		out = new PrintWriter(new BufferedWriter(new FileWriter(ime)));
		String dir  = "\\"+ re.getMainHomePath(true) +"\\",
			   kanta = "\\"+re.getHomeDriveName()+"$\\RECYCLER",
			   kanta2 = "\\"+re.getHomeDriveName()+"$\\$RECYCLE.BIN";
		for (int i=0; i < sysop.racunari.size() ;i++){
			if (sysop.racunari.get(i).isSelected()){
				if (sysop.racunari.get(i).isSelected()){
					if (i % 2 != 0 && par){
						out.println( "rd /S /Q \"\\\\"+ sysop.racunari.get(i).getText() + dir +
								KLForma.getInstance().target.parniPath.getText()+"\"");
					}
					if (i % 2 ==0 && nepar){
						out.println( "rd /S /Q \"\\\\"+ sysop.racunari.get(i).getText() + dir +
								KLForma.getInstance().target.neparniPath.getText()+"\"");
					}
					out.println( "rd /S /Q \"\\\\"+ sysop.racunari.get(i).getText() + kanta);
					out.println( "rd /S /Q \"\\\\"+ sysop.racunari.get(i).getText() + kanta2);
				}
			}
		}
		out.close();
	}
	
}
